
package com.usersmodule.dao;
import com.usersmodule.bean.TbPostUserMiddle;

import java.sql.*;
import java.util.*;
import util.*;
public class TbPostUserMiddleDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbPostUserMiddle tbPostUserMiddle=null;
    private List<TbPostUserMiddle> tbPostUserMiddleList=null;

    public boolean add(TbPostUserMiddle inTbPostUserMiddle){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_PostUserMiddle(PostId,UserId) values(?,?)";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbPostUserMiddle.getPostId());
        pt.setInt(2,inTbPostUserMiddle.getUserId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inPostUserMiddleId){
        return UtilDAO.del("Tb_PostUserMiddle", "PostUserMiddleId", inPostUserMiddleId);
    }
    public boolean edit(TbPostUserMiddle inTbPostUserMiddle){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_PostUserMiddle set PostId=?,UserId=?,CreateTime=getdate() where PostUserMiddleId=?";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbPostUserMiddle.getPostId());
        pt.setInt(2,inTbPostUserMiddle.getUserId());
        pt.setInt(3,inTbPostUserMiddle.getPostUserMiddleId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbPostUserMiddle getTbPostUserMiddleByPostUserMiddleId(int inPostUserMiddleId) {
        tbPostUserMiddle=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_PostUserMiddle where PostUserMiddleId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inPostUserMiddleId);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbPostUserMiddle = getTbPostUserMiddle(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbPostUserMiddle;
    }
    public List<TbPostUserMiddle> getTbPostUserMiddleAll() {
        tbPostUserMiddleList=new ArrayList<TbPostUserMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_PostUserMiddle";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbPostUserMiddleList.add(getTbPostUserMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbPostUserMiddleList;
    }
    public List<TbPostUserMiddle> getTbPostUserMiddleAll(VskPage vp) {
        tbPostUserMiddleList=new ArrayList<TbPostUserMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_PostUserMiddle";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_PostUserMiddle", "PostUserMiddleId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbPostUserMiddleList.add(getTbPostUserMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbPostUserMiddleList;
    }
    public TbPostUserMiddle getTbPostUserMiddle(ResultSet rs) throws Exception{
        TbPostUserMiddle tbPostUserMiddle = new TbPostUserMiddle();
        tbPostUserMiddle.setPostUserMiddleId(rs.getInt("PostUserMiddleId"));
        tbPostUserMiddle.setPostId(rs.getInt("PostId"));
        tbPostUserMiddle.setUserId(rs.getInt("UserId"));
        tbPostUserMiddle.setCreateTime(rs.getString("CreateTime"));
        return tbPostUserMiddle;
    }
}
